<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Promise.allSettled使用</title>
</head>
<body>
    <script>
        let hd = new Promise((resolve,reject) => {
            setTimeout(() => {
                reject("success");
            },2000)
        });

        let hdms = new Promise((resolve,reject) => {
            setTimeout(() => {
                resolve("成功");
            },2000)
        });

        // Promise.allSettled中成功或者失败都会执行，返回的状态不一样
        Promise.allSettled([hd,hdms]).then(
            value => console.log(value)
        );


        function ajax(url){
            return new Promise((resolve,reject) => {
                if(!/^http/.test(url)){
                    throw new ParamError("请求地址格式错误");
                }

                let xhr = new XMLHttpRequest();
                xhr.open("GET",url);
                xhr.send();
                xhr.onload = function(){
                    if(this.status == 200){
                        resolve(JSON.parse(this.response));
                    }else if(this.status == 404){
                        reject(new HttpError("请求参数错误"));
                    }else{
                        reject("加载失败");
                    }
                };
                xhr.onerror = function (){
                    reject(this);
                }
            })
        }

        // 根据类型获取不同的数据
        function query(param){
            let url = "http://localhost:8008/index.php/api/home/getJiaDianList?type=";
            const promises = param.map(value => {
                return ajax(`${url}${value}`)
            });
            return Promise.allSettled(promises);
        }

        query([1,2,4]).then(
            value => {
                let types = value.filter(val => val.status == "fulfilled");
                console.log(types);
            }
        )
    </script>
</body>
</html>